/**
 *  Copyright (c) 2006, Reliable Source, Inc. 
 *  All Rights Reserved.
 *
 */
package org.reliablesource.prism.ui.actions;

import org.eclipse.core.runtime.Preferences.IPropertyChangeListener;
import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.reliablesource.prism.ui.plugin.PrismPlugin;
import org.reliablesource.prism.ui.util.PrismUIConstants;

/**
 * CaptureScreenAction.java<br>
 * Created on: Mar 15, 2006<br>
 * 
 * @author Dennis Park 
 * 		<a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 * 
 * @since 1.0
 * 
 * @see IPropertyChangeListener
 */
public class CaptureScreenAction extends Action implements IPropertyChangeListener {
	
	private static Listener captureRequestListener;
	private static final int [] adapterList = { SWT.Arm, SWT.Selection, SWT.Activate, 
		SWT.MenuDetect, SWT.Expand, SWT.Collapse, SWT.DragDetect, 
		SWT.DefaultSelection, SWT.KeyUp, SWT.Dispose, SWT.Show};
	
//	SWT.Show
//	SWT.Dispose
	
	
	int USE_BUTTON = PrismUIConstants.MOUSE_MIDDLE;

	private static Display display;

	/**
	 * The constructor.
	 */
	public CaptureScreenAction() {
		display = Display.getDefault();
		captureRequestListener = new Listener() {
			public void handleEvent(final Event _evnt) {
				
				System.err.println("------------Start-----------");
				String _eventStringLiteral = PrismPlugin.getDefault().getEventLiteral(_evnt.type);
				
				System.err.println(_eventStringLiteral + " Event  :"+"\t"+ _evnt.toString());
				System.err.println("Widget context-id - "+PrismPlugin.getDefault().getWidgetContextString(_evnt.widget));
				System.err.println("widget.toString() : " + _evnt.widget.toString());
				
				if(_evnt.widget instanceof Control){
					System.err.println("widget handle: "+((Control)_evnt.widget).handle);
				}
				
				else if(_evnt.widget instanceof Menu){
					System.err.println("widget handle: "+((Menu)_evnt.widget).handle);
				}				
			}
		};
	}
	
	public void run() {
		
		Shell [] shells = display.getShells();

		int nShells = shells.length;
		StringBuffer buffer = new StringBuffer();		
		for(int i =0; i < nShells; i++){
			System.err.println("Shell " + i );
			PrismPlugin.getDefault().printCompositeContext(shells[i], buffer);
		}
		
		
		if (isChecked())
			addDisplayFilters();
		 else
			removeDisplayFilters();
	}

	private void readPrefrences() {
		String mouseButtonPref = PrismPlugin.getDefault()
				.getPreferenceStore().getString(PrismUIConstants.P_CAPTURE_TRIGGER);

		if (mouseButtonPref.equals(Integer.toString(PrismUIConstants.MOUSE_MIDDLE)))
			USE_BUTTON = PrismUIConstants.MOUSE_MIDDLE;
		if (mouseButtonPref.equals(Integer.toString(PrismUIConstants.MOUSE_RIGHT)))
			USE_BUTTON = PrismUIConstants.MOUSE_RIGHT;
		}
	

	/**
	 * We can use this method to dispose of any system
	 * resources we previously allocated.
	 */
	public void dispose() {
		PrismPlugin.getDefault().getPluginPreferences().removePropertyChangeListener(this);
		
		if (captureRequestListener == null)
			return;
		removeDisplayFilters();
		
	}

	public void init() {
		PrismPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(this);
		readPrefrences();
	}

	public void propertyChange(PropertyChangeEvent _event) {
		if( _event.getProperty().equals(PrismUIConstants.P_CAPTURE_TRIGGER) )
				readPrefrences();
	}
	
	public static void addDisplayFilters(){	
		int [] _events = getEvents();
		int _nEvents = _events.length;
		for(int i = 0; i < _nEvents; i++)
			addDisplayFilter(_events[i]);
	}
	public static void removeDisplayFilters(){
		int [] _events = getEvents();
		int _nEvents = _events.length;
		for(int i = 0; i < _nEvents; i++)
			removeDisplayFilter(_events[i]);
	}
	
	public static void removeDisplayFilter(int _eventType){
		display.removeFilter(_eventType,captureRequestListener);
	}
	
	public static void addDisplayFilter(int _eventType){
		display.addFilter(_eventType,captureRequestListener);
	}
	
	public static int [] getEvents(){
		return adapterList;
	}
	
}